package ru.mcfr.oxygen.framework.operations.table;

import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.access.AuthorTableAccess;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 18.03.11
 * Time: 18:17
 * To change this template use File | Settings | File Templates.
 */
public class SplitCellOperation implements AuthorOperation {
    public void doOperation(AuthorAccess authorAccess, ArgumentsMap argumentsMap) throws IllegalArgumentException, AuthorOperationException {
        AuthorDocumentController controller = authorAccess.getDocumentController();
        AuthorSchemaManager manager = authorAccess.getDocumentController().getAuthorSchemaManager();
        AuthorTableAccess tableContainer = authorAccess.getTableAccess();

        String selectedText = "";
        try {
            selectedText = authorAccess.getEditorAccess().getSelectedText();
        } catch (Exception e) {
        }

        try{
            int currentPosition = authorAccess.getEditorAccess().getCaretOffset();
            if (!selectedText.isEmpty())
                currentPosition = authorAccess.getEditorAccess().getSelectionStart();

            AuthorNode an = controller.getNodeAtOffset(currentPosition);
            while (!an.getName().equals("ячейка"))
                an = an.getParent();

            try{
                String idCurrentCell = ((AuthorElement) an).getAttribute("id").getValue();
                String colsStringValue = ((AuthorElement) an).getAttribute("colspan").getValue();
                if (!colsStringValue.equals("")){
                    int start = an.getStartOffset();
                    int end = an.getEndOffset();
                    int cols = Integer.valueOf(colsStringValue);
                    String xmlFragment = "";
                    String attrs = "";
                    for (int i = 0; i < ((AuthorElement) an).getAttributesCount(); i++){
                        String attrName = ((AuthorElement) an).getAttributeAtIndex(i);
                        if (attrName != "id" & attrName != "colspan")
                            attrs += attrName + "=\"" + ((AuthorElement) an).getAttribute(attrName).getValue() + "\" ";
                    }

                    if (!attrs.isEmpty())
                        attrs = attrs.substring(0,attrs.length() - 1);

                    controller.removeAttribute("colspan", (AuthorElement) an);

                    for (int k = 0; k < cols - 1; k++){
                        xmlFragment += "<ячейка " + attrs + "/>";
                    }

                    AuthorDocumentFragment aeFragment = controller.createNewDocumentFragmentInContext(xmlFragment, end + 1);
                    controller.insertFragment(end + 1, aeFragment);

                }
            }catch (Exception e){}


        } catch (Exception e) {}
    }

    public ArgumentDescriptor[] getArguments() {
        return new ArgumentDescriptor[0];  //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
